// 格式化时间
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

// 登录以后跳转到来源页
const loginRedirectTo = ()=>{
  // 重定向到来源页
  const redirect_to = wx.getStorageSync('redirect_to') || '/pages/index/index'

  //   判断是否tabbar页面
  const tabbars = [
    '/pages/index/index',
    '/pages/category/category',
    '/pages/cart/cart',
    '/pages/my/my',
  ]

  // 如果是tabbar页面
  if (tabbars.indexOf(redirect_to) != -1){
    wx.switchTab({
      url:redirect_to
    })
  }else {
    wx.redirectTo({
      url:redirect_to
    })
  }
}


const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

module.exports = {
  formatTime,
  loginRedirectTo
}
